#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 505;
const int M = 5e5 + 5;
int n, m;
int a[N][N];

int f[N * N];
int id(int x, int y) { return (x - 1) * m + y; }
void init() { rep(i, 1, n * m) f[i] = i; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
bool merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return false;
  f[fy] = fx;
  return true;
}
struct Edge {
  int u, v, w;
  bool operator<(Edge x) const { return w > x.w; }
} e[M];
int num;
void addedge(int x1, int y1, int x2, int y2) {
  int u = id(x1, y1), v = id(x2, y2);
  int w = min(a[x1][y1], a[x2][y2]);
  e[++num] = Edge{u, v, w};
}
int head[N * N];
int ver[N * N * 2], W[N * N * 2], Next[N * N * 2], tot;
void add(int u, int v, int w) {
  ++tot;
  ver[tot] = v;
  W[tot] = w;
  Next[tot] = head[u];
  head[u] = tot;
}
void kruskal() {
  sort(e + 1, e + num + 1);
  init();
  rep(i, 1, num) {
    if (merge(e[i].u, e[i].v)) {
      add(e[i].u, e[i].v, e[i].w);
      add(e[i].v, e[i].u, e[i].w);
    }
  }
}

int st[N * N][20], mn[N * N][20], dep[N * N];
void dfs(int u, int fu, int w) {
  dep[u] = dep[fu] + 1;
  st[u][0] = fu;
  mn[u][0] = w;
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    int w = W[i];
    if (v == fu) continue;
    dfs(v, u, w);
  }
}
void init_st() {
  dfs(1, 0, 0);
  rep(j, 1, 18) {
    rep(i, 1, n * m) {
      st[i][j] = st[st[i][j - 1]][j - 1];
      mn[i][j] = min(mn[i][j - 1], mn[st[i][j - 1]][j - 1]);
    }
  }
}
int query(int u, int v) {
  int res = 1e9;
  if (dep[u] < dep[v]) swap(u, v);
  int d = dep[u] - dep[v];
  per(j, 18, 0) {
    if (d & (1 << j)) res = min(res, mn[u][j]), u = st[u][j];
  }
  if (u == v) return res;
  per(j, 18, 0) {
    if (st[u][j] != st[v][j]) {
      res = min(res, mn[u][j]);
      res = min(res, mn[v][j]);
      u = st[u][j], v = st[v][j];
    }
  }
  res = min(res, mn[u][0]);
  res = min(res, mn[v][0]);
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, m) cin >> a[i][j];
  rep(i, 1, n) rep(j, 1, m) {
    if (i < n) addedge(i, j, i + 1, j);
    if (j < m) addedge(i, j, i, j + 1);
  }
  kruskal();  // build tree
  init_st();  // create st
  int q;
  cin >> q;
  while (q--) {
    int a, b, y, c, d, z;
    cin >> a >> b >> y >> c >> d >> z;
    int u = id(a, b), v = id(c, d);
    int x = query(u, v);
    if (x >= min(y, z))
      cout << abs(y - z) << endl;
    else
      cout << y + z - 2 * x << endl;
  }
  return 0;
}